package com.beagle.njddapp.weight.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;


import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.beagle.njddapp.R;
import com.beagle.njddapp.bean.owner.NjBean;
import com.beagle.njddapp.core.UserService;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author hxx
 * @date 2021-03-13 20:13
 */
public class NjDetailDIalog extends Dialog {

    @BindView(R.id.tv_number)
    TextView tvNumber;
    @BindView(R.id.tv_status)
    TextView tvStutas;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_location)
    TextView tvLocation;
    @BindView(R.id.iv_nj)
    ImageView ivNj;
    private NjBean njBean_in;
    private onNjJiaYouClaaBack onNjJiaYouClaaBack;

    public void setOnNjJiaYouClaaBack(NjDetailDIalog.onNjJiaYouClaaBack onNjJiaYouClaaBack) {
        this.onNjJiaYouClaaBack = onNjJiaYouClaaBack;
    }

    public NjDetailDIalog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_nj_detail);
        ButterKnife.bind(this);
        if (getWindow() != null) {
            //按空白处不能取消动画
            setCanceledOnTouchOutside(true);
            //设置window背景，默认的背景会有Padding值，不能全屏。当然不一定要是透明，你可以设置其他背景，替换默认的背景即可。
            getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            //一定要在setContentView之后调用，否则无效
            getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            getWindow().setWindowAnimations(R.style.DialogAnim);
        }
    }
    public void update(NjBean njBean){
        njBean_in=njBean;
        tvName.setText(UserService.getInstance().getUserInfoBean().getUserName());
        tvNumber.setText(njBean.getMeshNum());
        GeocodeSearch geocoderSearch = new GeocodeSearch(getContext());
        geocoderSearch.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener(){

            @Override
            public void onGeocodeSearched(GeocodeResult result, int rCode) {


            }

            @Override
            public void onRegeocodeSearched(RegeocodeResult result, int rCode) {
                try {
                    tvLocation.setText(result.getRegeocodeAddress().getFormatAddress());
                }catch (Exception e){

                }

            }});
        com.amap.api.services.core.LatLonPoint lp = new  com.amap.api.services.core.LatLonPoint(njBean.getLatitude(),njBean.getLongitude());
        RegeocodeQuery query = new RegeocodeQuery(lp,200,GeocodeSearch.AMAP);
        geocoderSearch.getFromLocationAsyn(query);
        Glide.with(getContext()).load(njBean.getFrontPictureUrl()).into(ivNj);
        if ("1".equals(njBean.getStatus())){
            tvStutas.setText("运行中");
            tvStutas.setBackground(getContext().getResources().getDrawable(R.drawable.bg_nj_run));
            tvStutas.setTextColor(Color.parseColor("#00A47C"));
        }else if ("2".equals(njBean.getStatus())){
            tvStutas.setText("静止中");
            tvStutas.setBackground(getContext().getResources().getDrawable(R.drawable.bg_nj_stop));
            tvStutas.setTextColor(Color.parseColor("#FF5C58"));
        }

    }
    @OnClick({R.id.container,R.id.tv_guiji,R.id.tv_jiayou})
    public void onCLick(View view){
        switch (view.getId()){
            case R.id.container:
                dismiss();
                break;
            case R.id.tv_guiji:
                ToastUtils.showShort("此功能即将开放");
                break;
            case R.id.tv_jiayou:
                onNjJiaYouClaaBack.jiayou(njBean_in);
                dismiss();
                break;

        }
    }
    public interface  onNjJiaYouClaaBack{
        void jiayou (NjBean njBean);
    }


}
